package com.comtime.smartbox;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.comtime.greendaodemo.RemindInfo;
import com.comtime.greendaoutils.DataBaseUtil;
import com.comtime.home.MapActivity;
import com.comtime.service.MyService;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.MySharedPreferences;
import com.comtime.view.MyDialog;
import com.comyou.customviews.SlideListView;

import java.util.List;

public class Activity_2 extends BaseActivity {

	SlideListView listView;

	DataBaseUtil dataBaseUtil;
	List<RemindInfo> remindInfos;
	MySharedPreferences mySharedPreferences;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_2);
		mySharedPreferences = MySharedPreferences.getInstance(this);
		dataBaseUtil = DataBaseUtil.getInstance(this);

		init();
		updateUI();
		regist();
	}

	void init() {
		listView = (SlideListView) findViewById(R.id.listview);
		listView.setRightBtnWidth(dp2px(70));
		listView.setOnItemClickListener(itemClickListener);
		listView.setAdapter(adapter);

	}

	void regist() {
		IntentFilter filter = new IntentFilter(MyService.ACTION_UPDATE_UI);
		filter.addAction(MyService.ACTION_CONNECTED);
		filter.addAction(MyService.ACTION_DISCONNECTED);
		filter.addAction(MyService.ACTION_LOW_BATTERYVALUE);
		registerReceiver(receiver, filter);
	}

	String action = "";
	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			action = intent.getAction();
			if (action.equals(MyService.ACTION_UPDATE_UI)) {
				updateUI();
			} else if (action.equals(MyService.ACTION_DISCONNECTED)) {
				updateUI();
			} else if (action.equals(MyService.ACTION_CONNECTED)) {
				updateUI();
			} else if (action.equals(MyService.ACTION_LOW_BATTERYVALUE)) {
				updateUI();
			}

		}
	};

	/**
	 * 改变界面
	 */
	void updateUI() {
		if (remindInfos != null) {
			remindInfos.clear();
			adapter.notifyDataSetChanged();
		}
		remindInfos = dataBaseUtil.loadRemindInfoByShow(
				mySharedPreferences.getUserId(),
				mySharedPreferences.getBluAdress());

		listView.deleteItem();
		adapter.notifyDataSetChanged();
	}

	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long arg3) {
			int type = remindInfos.get(position).getStyle();
			if (type == 1) {
				Intent intent = new Intent(Activity_2.this, MapActivity.class);
				intent.putExtra(MyService.VALUE_TAG, 1);
				intent.putExtra(MyService.VALUE_INFO, remindInfos.get(position));
				startActivity(intent);
			}

		}
	};

	int deletePosition = 0;
	BaseAdapter adapter = new BaseAdapter() {

		@Override
		public View getView(int position, View view, ViewGroup group) {
			WidViewHolder widViewHolder = null;

			RemindInfo info = remindInfos.get(position);

			int type = getItemViewType(position);
			if (view == null) {
				widViewHolder = new WidViewHolder();
				view = LayoutInflater.from(Activity_2.this).inflate(
						R.layout.list_item_remind, null);
				widViewHolder.image_photo_wid = (ImageView) view
						.findViewById(R.id.image_photo_wid);
				widViewHolder.tv_title_wid = (TextView) view
						.findViewById(R.id.tv_title_wid);
				widViewHolder.tv_time_wid = (TextView) view
						.findViewById(R.id.tv_time_wid);
				widViewHolder.tv_address_wid = (TextView) view
						.findViewById(R.id.tv_address_wid);

				widViewHolder.layout_nor = (RelativeLayout) view
						.findViewById(R.id.layout_nor);

				widViewHolder.layout_wid = (RelativeLayout) view
						.findViewById(R.id.layout_wid);

				widViewHolder.image_photo = (ImageView) view
						.findViewById(R.id.image_photo);
				widViewHolder.tv_title = (TextView) view
						.findViewById(R.id.tv_title);
				widViewHolder.tv_time = (TextView) view
						.findViewById(R.id.tv_time);
				widViewHolder.btn_delete = (Button) view
						.findViewById(R.id.ib_delete);

				view.setTag(widViewHolder);
				// if (type == 1) {
				//
				// } else {
				//
				// norViewHolder = new NorViewHolder();
				// view = LayoutInflater.from(Activity_2.this).inflate(
				// R.layout.list_item_remind_nor, null);
				// norViewHolder.image_photo = (ImageView) view
				// .findViewById(R.id.image_photo);
				// norViewHolder.tv_title = (TextView) view
				// .findViewById(R.id.tv_title);
				// norViewHolder.tv_time = (TextView) view
				// .findViewById(R.id.tv_time);
				// norViewHolder.btn_delete = (Button) view
				// .findViewById(R.id.ib_delete);
				// view.setTag(norViewHolder);
				// }

			} else {
				widViewHolder = (WidViewHolder) view.getTag();
				// if (type == 1) {
				//
				// } else {
				// norViewHolder = (NorViewHolder) view.getTag();
				// }

			}
			// 1断开;2 连接提醒;3 低电量
			switch (type) {
			case 1:
				widViewHolder.layout_wid.setVisibility(View.VISIBLE);
				widViewHolder.layout_nor.setVisibility(View.GONE);

				widViewHolder.image_photo_wid
						.setImageResource(R.drawable.icon_bag_nor);
				widViewHolder.tv_title_wid.setText(getText(
						R.string.box_disconnet).toString());
				widViewHolder.tv_time_wid.setText(remindInfos.get(position)
						.getTime());
				Log.e("RZC",remindInfos.get(position).getAddress());
				if (remindInfos.get(position).getAddress().equals("")) {
					Log.e("RZC null",remindInfos.get(position).getAddress());
					widViewHolder.tv_address_wid.setText(getText(
							R.string.no_address).toString());
				} else {
					Log.e("RZC adr",remindInfos.get(position).getAddress());
					widViewHolder.tv_address_wid.setText(remindInfos.get(
							position).getAddress());
				}

				break;
			case 2:
				widViewHolder.layout_wid.setVisibility(View.GONE);
				widViewHolder.layout_nor.setVisibility(View.VISIBLE);

				widViewHolder.image_photo
						.setImageResource(R.drawable.icon_bag_con);
				widViewHolder.tv_title.setText(getText(R.string.box_connet)
						.toString());
				widViewHolder.tv_time.setText(remindInfos.get(position)
						.getTime());
				break;
			case 3:
				widViewHolder.layout_wid.setVisibility(View.GONE);
				widViewHolder.layout_nor.setVisibility(View.VISIBLE);
				widViewHolder.image_photo
						.setImageResource(R.drawable.icon_battery_low);
				widViewHolder.tv_title
						.setText(getText(R.string.box_low_battery).toString());
				widViewHolder.tv_time.setText(remindInfos.get(position)
						.getTime());
				break;
			default:
				break;
			}
			widViewHolder.btn_delete.setTag(position);
			widViewHolder.btn_delete.setOnClickListener(clickListener);

			return view;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		// @Override
		// public int getViewTypeCount() {
		// return 3;
		// }

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return remindInfos == null ? 0 : remindInfos.size();
		}

		OnClickListener clickListener = new OnClickListener() {

			@Override
			public void onClick(View view) {

				switch (view.getId()) {
				case R.id.ib_delete:
					deletePosition = (Integer) view.getTag();
					showDeleteDialog(getText(R.string.delete_remind).toString());

					break;

				default:
					break;
				}

			}
		};

		@Override
		public int getItemViewType(int position) {
			return remindInfos.get(position).getStyle();
		}

		@Override
		public RemindInfo getItem(int position) {
			return remindInfos.get(position);
		}
	};

	private class WidViewHolder {
		//
		ImageView image_photo_wid;
		TextView tv_title_wid, tv_time_wid, tv_address_wid;
		Button btn_delete;

		RelativeLayout layout_wid, layout_nor;

		ImageView image_photo;
		TextView tv_title;
		TextView tv_time;
	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}

	// 确定删除
	void showDeleteDialog(String str) {
		final MyDialog myDialog = new MyDialog(Activity_2.this);
		myDialog.setContent(str);
		myDialog.getLeftButton().setText(getText(R.string.cancel));
		myDialog.getRighrButton().setText(getText(R.string.ok));
		myDialog.getLeftButton().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				myDialog.dismiss();

			}
		});
		myDialog.getRighrButton().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				myDialog.dismiss();
				// 删除记录
				RemindInfo remindInfo = remindInfos.get(deletePosition);
				Log.e("wori", " remindInfo :" + (remindInfo == null)
						+ " deletePosition:" + deletePosition + " "
						+ (remindInfo.getRemind_id() == null));
				if (remindInfo.getRemind_id() !=null&&remindInfo.getRemind_id()> 0) {
					remindInfo.setUpflag(2);
					dataBaseUtil.saveRemindInfo(remindInfo);
				} else {
					dataBaseUtil.deleteRemindInfo(remindInfo.getId());
				}
				remindInfos.remove(deletePosition);
				listView.deleteItem();
				adapter.notifyDataSetChanged();

			}
		});

		myDialog.show();
	}
}
